package com.llpy.Service.impl;

import com.llpy.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        com.llpy.pojo.User admin = userMapper.findByUsername(username);
        System.out.println("通过持久层进行查询，结果=" + admin);
        GrantedAuthority grantedAuthority= () -> "roladmin";

        List<GrantedAuthority> grantedAuthorities=new ArrayList<>();
        grantedAuthorities.add(grantedAuthority);
        System.out.println(grantedAuthorities);

        grantedAuthorities.add(grantedAuthority);
        //这里可以实现从数据库取，我为了偷懒，就没这么做  - -
        UserDetails userDetails = User.builder()
                .username(admin.getUsername())
                .authorities(grantedAuthorities)
                .build();

        return userDetails;
    }
}
